package 算法面试;

public class 字符串中的第一个唯一字符 {
    public static void main(String[] args) {
        字符串中的第一个唯一字符 test = new 字符串中的第一个唯一字符();
        test.firstUniqChar("leetcode");
    }

    public int firstUniqChar(String s) {
        int[] chars = new int[26];
        char[] charArray = s.toCharArray();
        for (int index = 1; index <= charArray.length; index++) {
            char c = charArray[index - 1];
            int charsIndex = c - 'a';
            int i = chars[charsIndex];
            if (i == 0) {
                chars[charsIndex] = index;
            } else if (i > 0) {
                chars[charsIndex] = -1;
            }
        }

        int minIndex = -1;
        for (int i = 0; i < chars.length; i++) {
            int aChar = chars[i];
            if (aChar > 0 && (
                    minIndex < 0 || aChar - 1 < minIndex
            )) {
                minIndex = aChar - 1;
            }
        }
        return minIndex;
    }

}
